package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("department")
public class DepartmentController {
	@Autowired
	IDepartmentService service;

	@RequestMapping("")
	public String index(){
		return "department/list";
	}
	@RequestMapping("queryDepts")
	@ResponseBody
	public List<Department> queryDepts(){
		return service.selectAll();
	}


	@RequestMapping("input")
	public String input(Long id,Model model){
		if(id!=null){
			Department department = service.selectByPrimaryKey(id);
			model.addAttribute("department", department);
		}
		return "department/input";
	}

	@RequestMapping("delete")
	@ResponseBody
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if(id!=null){
				service.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}

		return result;
	}

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(Department department){
		JsonResult result = new JsonResult();
		try {
			if(department.getId()!=null){
				service.updateByPrimaryKey(department);
			}else{
				service.insert(department);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("操作失败");
		}
		return result;
	}
}
